package com.zx.controller;

import com.zx.core.wrapper.ObjectWrapper;
import com.zx.core.wrapper.PageWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.model.Course;
import com.zx.param.CourseParam;
import com.zx.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * description: 课程服务 <br>
 * create: 2024-10-25 12:01
 * </p>
 *
 * @author zhou  xun
 */
@RestController
@RequestMapping("/courses") // 统一路径前缀
public class CourseController {
    private final CourseService courseService;

    @Autowired
    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    /**
     * 根据ID查找课程
     *
     * @param id 课程ID
     * @return 包装后的课程对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<ObjectWrapper<Course>> findCourseById(@PathVariable("id") Long id) {
        ObjectWrapper<Course> response = courseService.findCourseById(id);
        return ResponseEntity.ok(response);
    }

    /**
     * 保存课程
     *
     * @param course 课程对象
     * @return 包装后的响应结果
     */
    @PostMapping
    public ResponseEntity<ResponseWrapper> saveCourse(@RequestBody CourseParam course) {
        ResponseWrapper response = courseService.saveCourse(course);
        return ResponseEntity.ok(response);
    }

    /**
     * 根据ID更新课程
     *
     * @param course 课程对象
     * @return 包装后的响应结果
     */
    @PutMapping("/{id}")
    public ResponseEntity<ResponseWrapper> updateCourseById(@RequestBody CourseParam course) {
        ResponseWrapper response = courseService.updateCourseById(course);
        return ResponseEntity.ok(response);
    }

    /**
     * 根据ID删除课程
     *
     * @param id 课程ID
     * @return 包装后的响应结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ResponseWrapper> deleteCourseById(@PathVariable("id") Long id) {
        ResponseWrapper response = courseService.deleteCourseById(id);
        return ResponseEntity.ok(response);
    }

    /**
     * 分页查询课程
     *
     * @param param 查询参数
     * @return 包装后的分页结果
     */
    @PostMapping("/page")
    public ResponseEntity<PageWrapper<Course>> findCourseByPage(@RequestBody CourseParam param) {
        PageWrapper<Course> pageResponse = courseService.findCourseByPage(param);
        return ResponseEntity.ok(pageResponse);
    }
}